package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.Province;
import com.yoyo.itravel.constants.MsgConstant;
import com.yoyo.itravel.dao.IProvinceDao;
import com.yoyo.itravel.exceptions.ProvinceException;
import com.yoyo.itravel.service.IProvinceService;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 省份操作服务层接口实现类
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/18 15:49
 */
@Service
public class ProvinceServiceImpl implements IProvinceService {

    // 声明省份持久层接口对象
    @Autowired
    private IProvinceDao provinceDao;

    /**
     * @Description 新增省份
     *
     * @Param province
     * @Return boolean
     */
    @Override
    public boolean addSightTheme(Province province) throws ProvinceException {
        /* 判断province对象是否为null */
        if (province == null) {
            throw new ProvinceException("传入的省份对象为null...");
        }

        /* 判断该省份名称是否已经存在 */
        // 获得省份名称
        String provinceName = province.getProvinceName();

        try {
            // 通过省份名称查找省份，并判断省份名称是否存在
            if (provinceDao.selectByProvinceName(provinceName) != null) {
                throw new ProvinceException("该省份名称已存在，请重新命名！");
            }
            // 添加省份
            provinceDao.insert(province);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProvinceException(MsgConstant.INTERNAL_ERR);
        }
        return false;
    }

    /**
     * @Description 查找所有的省份
     *
     * @Param
     * @Return java.util.List<com.yoyo.itravel.bean.Province>
     */
    @Override
    public List<Province> findAllProvince() throws ProvinceException {
        try {
            return provinceDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProvinceException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @Description 通过景点名称查找景点
     *
     * @Param province
     * @Return com.yoyo.itravel.bean.Province
     */
    @Override
    public Province findByName(String province) throws ProvinceException {
        if (StringUtils.isEmpty(province)) {
            throw new ProvinceException("请选择省份！");
        }
        try {
            return provinceDao.selectByProvinceName(province);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProvinceException(MsgConstant.INTERNAL_ERR);
        }
    }
}
